layui.use(['form', 'layer', 'table', 'laydate', 'func'], function () {
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;
    var laydate = layui.laydate;
    var func = layui.func;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    /**
     * 初始化表格
     */
    var SmPlan = {
        tableId: "SmPlanTable" //表格id
    };

    /**
     * 初始化表格的列
     */
    SmPlan.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'userName', align: "center", sort: true, title: '用户名'},
            {field: 'name', align: "center", sort: true, title: '计划名称'},
            {field: 'type', align: "center", sort: true, title: '计划类型',
                templet: function(object){
                    if(0 == object.type){
                        return '<span class="layui-badge layui-bg-green">一月</span>';
                    }else if(1 == object.type){
                        return '<span class="layui-badge layui-bg-blue">一季</span>';
                    }else if(2 == object.type){
                        return '<span class="layui-badge layui-bg-orange">半年</span>';
                    } else if(3 == object.type){
                        return '<span class="layui-badge">一年</span>';
                    }
                }
            },
            {field: 'rule', align: "center", sort: true, title: '规则名称',
                templet: function(object){
                    if('' == object.rule || null == object.rule || undefined == object.rule){
                        return '-';
                    }else{
                        return object.rule;
                    }
                }
            },
            {field: 'patternType', align: "center", sort: true, title: '图型类型',
                templet: function(object){
                    if(0 == object.patternType){
                        return '<span class="layui-badge layui-bg-green">心型</span>';
                    }else if(1 == object.patternType){
                        return '<span class="layui-badge layui-bg-blue">蝴蝶型</span>';
                    }else if(2 == object.patternType){
                        return '<span class="layui-badge">方形</span>';
                    }
                }
            },
            {field: 'money', align: "center", sort: true, title: "打卡累计金额",
                templet: function(object){
                    return "<span style='color:#FF5722'>" + object.money + "</span>元";
                }
            },
            {field: 'amount', align: "center", sort: true, title: "总天数金额",
                templet: function(object){
                    return "<span style='color:#1E9FFF'>" + object.amount + "</span>元";
                }
            },
            {field: 'maxMoney', align: "center", sort: true, title: "最大存钱数",
                templet: function(object){
                    return "<span style='color:#009688'>" + object.maxMoney + "</span>元";
                }
            },
            {field: 'dayNum', align: "center", sort: true, title: "连续打卡天数"},
            {field: 'status', align: "center", sort: true, title: '状态',
                templet: function(object){
                    if(0 == object.status){
                        return '<span class="layui-badge layui-bg-green">进行中</span>';
                    }else if(1 == object.status){
                        return '<span class="layui-badge">已放弃</span>';
                    }
                }
            },
            {field: 'remark', align: "center", sort: true, title: '备注',
                templet: function(object){
                    if('' == object.remark || null == object.remark || undefined == object.remark){
                        return '-';
                    }else{
                        return object.remark;
                    }
                }
            },
            {field: 'createDate', align: "center", sort: true, title: '创建时间'},
            {field: 'endDate', align: "center", sort: true, title: '结束时间'}
        ]];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + SmPlan.tableId,
        url: Feng.ctxPath + '/web/smPlan/list',
        page: true,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        height: "full-98",
        cellMinWidth: 100,
        cols: SmPlan.initColumn()
    });

    //日期范围
    laydate.render({
        elem: '#createDate'
        ,range: true
    });

    /**
     * 点击查询按钮
     */
    SmPlan.search = function () {
        var queryData = {};
        queryData['userName'] = $("#userName").val();
        queryData['rule'] = $("#rule").val();
        queryData['type'] = $("#type").val();
        queryData['patternType'] = $("#patternType").val();
        queryData['status'] = $("#status").val();
        queryData['createDate'] = $("#createDate").val();
        table.reload(SmPlan.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 点击重置按钮
     */
    SmPlan.reset = function () {
        $("#userName").val("");
        $("#rule").val("");
        $("#type").val("-1");
        $("#patternType").val("-1");
        $("#status").val("-1");
        $("#createDate").val("");

        form.render();

        var queryData = {};
        queryData['userName'] = "";
        queryData['rule'] = "";
        queryData['type'] = "-1";
        queryData['patternType'] = "-1";
        queryData['status'] = "-1";
        queryData['createDate'] = "";
        SmPlan.search();
    };

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        SmPlan.search();
    });

    // 重置按钮点击事件
    $('#btnReset').click(function () {
        SmPlan.reset();
    });

    // 工具条点击事件
    // table.on('tool(' + UserGoldLog.tableId + ')', function (obj) {
    //     var data = obj.data;
    //     var layEvent = obj.event;
    //
    //     if (layEvent === 'edit') {
    //         UserGoldLog.onEdit(data);
    //     } else if (layEvent === 'delete') {
    //         UserGoldLog.onDelete(data);
    //     }
    // });

});

$(function () {
    var panehHidden = false;
    if ($(this).width() < 769) {
        panehHidden = true;
    }
    $('#myContiner').layout({initClosed: panehHidden, west__size: 260});
});